﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using Microsoft.Practices.Unity;
using Vit.Web.Services;

namespace Vit.Web.DataAnnotations
{
    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
    public class DisplayFormatAttribute : System.ComponentModel.DataAnnotations.DisplayFormatAttribute
    {
        public DisplayFormatAttribute()
        {
        }

        public DisplayFormatAttribute(string format)
            : this(format, "")
        {
        }

        public DisplayFormatAttribute(string format, string nullDisplayText)
        {
            this.ConvertEmptyStringToNull = true;
            IUnityContainer unity = ((VitApplication)HttpContext.Current.ApplicationInstance).UnityContainer;
            ILocalizationService localize = unity.Resolve<ILocalizationService>();
            if (localize != null)
            {
                DataFormatString = localize.Resource(Area, format);
                NullDisplayText = localize.Resource(Area, nullDisplayText);
            }
            else
            {
                DataFormatString = format;
                NullDisplayText = nullDisplayText;
            }
        }

        public string Area { get; set; }
    }
}
